package com.sparrow.common.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalMemoryLeakExample {
    static class MyObject {
        private byte[] data = new byte[10 * 1024 * 1024]; // 10MB
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(1);

        for (int i = 0; i < 100; i++) {
            executor.submit(() -> {
                ThreadLocal<MyObject> threadLocal = new ThreadLocal<>();
                threadLocal.set(new MyObject());

                // 不清理 ThreadLocal
                // threadLocal.remove();

                System.out.println("ThreadLocal set done");
            });

            Thread.sleep(100); // 模拟任务间隔
        }

        executor.shutdown();
        System.out.println("All tasks submitted");
    }
}
